/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchPanel.java
 *
 * Created on Jun 17, 2009, 8:18:12 AM
 */

package matpred;

import java.awt.Component;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import matpred.search.*;
import utilities.BusyDialog;
import utilities.PredictionSearch;
import utilities.entities.Result;

/**
 *
 * @author Demola
 */
public class SearchPanel extends javax.swing.JPanel {

    /** Creates new form SearchPanel */
    @SuppressWarnings("empty-statement")
    public SearchPanel() {
        initComponents();

        search0 = new SMatch("Current Matches");
        search1 = new SType("Type");
        search2 = new CRange("Prediction Search Calendar", true);
        search3 = new DRange("Side A Date Range");
        search4 = new DRange("Side B Date Range");
        search5 = new DRange("Return Code Date Range");
        search6 = new DRange("Unnatural Date Range");
        search7 = new CRange("Search Result Calendar", false);
        search8 = new DRange("Compare Date Range");
        search9 = new NRange("Number Range");
        search10 = new NLevel("Search Number Levels");
        search11 = new SearchP();
        search12 = new SearchP();
        search13 = new SearchP();

        searchVars = new Search [] {search0, search1, search2, search3, search4, search5, search6,
                                    search7, search8, search9, search10, search11, search12, search13};

        jTabbedPane1.addTab("Current Matches",(Component) searchVars[0]);
        jTabbedPane1.addTab("Type",(Component) searchVars[1]);
        jTabbedPane1.addTab("Prediction Calendar",(Component) searchVars[2]);
        jTabbedPane1.addTab("Side A Date Range",(Component) searchVars[3]);
        jTabbedPane1.addTab("Side B Date Range",(Component) searchVars[4]);
        jTabbedPane1.addTab("Return Code Date Range",(Component) searchVars[5]);
//        jTabbedPane1.addTab("Unnatural Date Range",(Component) searchVars[6]);
        jTabbedPane1.addTab("Search Result Calendar",(Component) searchVars[7]);
        jTabbedPane1.addTab("Compare Date Range",(Component) searchVars[8]);
        jTabbedPane1.addTab("Number Range",(Component) searchVars[9]);
        jTabbedPane1.addTab("Search Number Levels",(Component) searchVars[10]);
//        jTabbedPane1.addTab("Rating Categories",(Component) searchVars[11]);
//        jTabbedPane1.addTab("Auto Search Rating",(Component) searchVars[12]);
//        jTabbedPane1.addTab("Result Rating",(Component) searchVars[13]);
    }

    public static SearchPanel getSearchPanel(){
        if(pnl == null){
            pnl = new SearchPanel();
        }
        return pnl;
    }

    public Search[] getSearchVar(){
        return searchVars;
    }

    public boolean checkValues(){
        for(int i = 0; i < searchVars.length; i++){
            if(!searchVars[i].checkValues()){
//                JOptionPane.showMessageDialog(null, searchVars[i].getErrorMessage(), "Incomplete Parameter",
//                        JOptionPane.ERROR_MESSAGE);
//                jTabbedPane1.setSelectedIndex(i);
//                return false;
            }
        }
        return true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jButton1 = new javax.swing.JButton();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jTabbedPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        jTabbedPane1.setFont(new java.awt.Font("Tahoma", 0, 14));

        jButton1.setText("Search");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 745, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(282, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        for(int i = 0; i < searchVars.length; i++){
            searchVars[i].refreshUI();
        }
    }//GEN-LAST:event_formComponentShown

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(checkValues()){
            try{
                final BusyDialog busy = new BusyDialog(null, true);
                final PredictionSearch ps = new PredictionSearch();
                SwingWorker<List<Result>, Void> sw = new SwingWorker() {
                    @Override
                    protected List<Result> doInBackground() throws Exception {
                        return ps.analyze();
                    }

                    @Override
                    protected void done(){
                        System.out.println("finished");
                        busy.setVisible(false);
                    }
                };
                sw.execute();
                busy.setVisible(true);

                if(busy.isCancel()){
                    sw.cancel(true);
                    JOptionPane.showMessageDialog(null, "Search cancelled");
                    return;
                }
                List<Result> result = sw.get();
                if(result == null || result.isEmpty()){
                    JOptionPane.showMessageDialog(null, "No result Found", "No Result", JOptionPane.ERROR_MESSAGE);
                }else{
                    new ResultDialog(result).setVisible(true);
                }
            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, "No result Found", "No Result E", JOptionPane.ERROR_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_jButton1MouseClicked


    private Search search0;
    private Search search1;
    private Search search2;
    private Search search3;
    private Search search4;
    private Search search5;
    private Search search6;
    private Search search7;
    private Search search8;
    private Search search9;
    private Search search10;
    private Search search11;
    private Search search12;
    private Search search13;
    private Search [] searchVars;

    private static SearchPanel pnl;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

}
